package com.zrrd.yunchmall.sale.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.sale.entity.FlashPromotion;
import com.zrrd.yunchmall.sale.service.IFlashPromotionService;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apiguardian.api.API;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;

/**
 * <p>
 * 限时购表 前端控制器
 * </p>
 *
 * @author LYY
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/flash")
@Api(tags = "营销服务-秒杀活动")
public class FlashPromotionController {

    @Autowired
    private IFlashPromotionService promotionService;

    @ApiOperation("查询秒杀活动列表")
    @GetMapping("/list")
    public ResponseResult list(@RequestParam int pageSize,
                               @RequestParam int pageNum,String title){
        QueryWrapper queryWrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(title)){
            queryWrapper.like("title",title);
        }
        Page<FlashPromotion>pageInfo=promotionService.page(new Page<>(pageNum,pageSize),queryWrapper);
        return new ResponseResult<>(200,"查询成功!",pageInfo);
    }

    @ApiOperation("修改秒杀活动状态")
    @PostMapping ("/update/status/{id}")
    public ResponseResult updateStatus(@PathVariable int id,
                                       @RequestParam @ApiParam("秒杀上下线状态")int status){
        UpdateWrapper updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("status",status);
        updateWrapper.in("id",id);
        promotionService.update(updateWrapper);
        return new ResponseResult<>(200,"修改成功!");
    }
    @ApiOperation("删除秒杀活动")
    @PostMapping ("/delete/{id}")
    public ResponseResult delete(@PathVariable int id){
        promotionService.removeById(id);
        return new ResponseResult<>(200,"删除成功!");
    }
    @ApiOperation("添加秒杀活动")
    @PostMapping ("/create")
    public ResponseResult create(@RequestBody FlashPromotion flashPromotion){
        promotionService.save(flashPromotion);
        return new ResponseResult<>(200,"添加成功!");
    }
    @ApiOperation("编辑秒杀活动")
    @PostMapping ("/update/{id}")
    public ResponseResult update(@PathVariable int id,@RequestBody FlashPromotion flashPromotion){
        UpdateWrapper updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        promotionService.update(flashPromotion,updateWrapper);
        return new ResponseResult<>(200,"修改成功!");
    }


}
